using System;
using System.Collections.Generic;
using UnityEngine;

public class TurnBaseManager : MonoBehaviour
{
    [SerializeField]private bool isPlayerTurn = false;
    [SerializeField]private bool isEnemyTurn = false;
    
    public bool battleEnd = true;

    [SerializeField]private float timeCounter;
    public float playerTurnDuration;
    public float enemyTurnDuration;
    
    public ObjectEventSO playerTurnBeginEvent;
    public ObjectEventSO enemyTurnBeginEvent;
    public ObjectEventSO enemyTurnEndEvent;

    public GameObject Player;
    private void Update()
    {
        if (battleEnd)
            return;
        if (isEnemyTurn)
        {
            timeCounter += Time.deltaTime;
            if (timeCounter >= enemyTurnDuration)
            {
                timeCounter = 0;
                //敌人回合结束
                EnemyTurnEnd();
                //玩家回合开始
                isPlayerTurn = true;
            }
        }

        if (isPlayerTurn)
        {
            timeCounter += Time.deltaTime;
            if (timeCounter >= playerTurnDuration)
            {
                isPlayerTurn = false;
                timeCounter = 0;
                //玩家回合开始
                PlayerTurnBegin();
            }
        }
    }

    [ContextMenu("新游戏开始")]
    public void GameStart()
    {
        isPlayerTurn = true;
        isEnemyTurn = false;
        battleEnd = false;
        timeCounter = 0;
    }

    public void PlayerTurnBegin()
    {
        playerTurnBeginEvent.RaisEvent(null,this);
    }
    public void EnemyTurnBegin()
    {
        isEnemyTurn = true;
        enemyTurnBeginEvent.RaisEvent(null,this);
    }
    public void EnemyTurnEnd()
    {
        isEnemyTurn = false;
        enemyTurnEndEvent.RaisEvent(null,this);
    }

    public void OnRoomLoadEvenr(object obj)
    {
        Room room = obj as Room;
        switch (room.roomDataSO.roomType)
        {
            case RoomType.MinorEnemy:
            case RoomType.EliteEnemy:
            case RoomType.Boss:
                Player.SetActive(true);
                GameStart();
                break;
            case RoomType.Treasure:
            case RoomType.Shop:
                Player.SetActive(false);
                break;
            case RoomType.RestRoom:
                Player.SetActive(true);
                Player.GetComponent<PlayerAnimation>().SetSleepAnimation();
                break;
        }
    }

    public void OnLoadMapEvent()
    {
        battleEnd = true;
        Player.SetActive(false);
    }

    public void NewGame()
    {
        Player.GetComponent<Player>().NewGame();
    }
}
